﻿<div id="gridPositions"></div>
<div id="dlgAddPosition" style="display:none;padding-left:10px;">
    <form id="formAddPosition"></form>
</div>
<div id="dlgUpdatePosition" style="display:none;padding-left:10px;">
    <form id="formUpdatePosition"></form>
</div>

<script type="text/javascript">
    var gridPositions = null;
    var dlgAddPosition = null;
    var dlgUpdatePosition = null;
    var formAddPosition = null;
    var formUpdatePosition = null;

    function addPosition() {
        $.post('/API/Position/Create', formAddPosition.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('添加成功', '提示');
                loadPositions();
                dlgAddPosition.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function updatePosition() {
        $.post('/API/Position/Update', formUpdatePosition.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('修改成功', '提示');
                loadPositions();
                dlgUpdatePosition.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }
    
    function deletePosition(row) {
        $.post('/API/Position/Delete', row,
            function (data) {
                if (data.code == 0) {
                    $.ligerDialog.success('删除成功', '提示');
                    loadPositions();
                } else {
                    $.ligerDialog.error(data.msg, '错误');
                }
            }, 'json');
    }

    function showDlgAddPosition() {
        if (dlgAddPosition) {
            dlgAddPosition.show();
        }
        else {
            dlgAddPosition = $.ligerDialog.open({
                target: $('#dlgAddPosition'),
                width: 320,
                height: 120,
                title: '添加新职位',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formAddPosition.valid()) {
                                addPosition();
                            }
                            else {
                                formAddPosition.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgAddPosition.hide();
                        }
                    }
                ]
            });
        }
    }

    function showDlgUpdatePosition(row) {
        if (dlgUpdatePosition) {
            dlgUpdatePosition.show();
        }
        else {
            dlgUpdatePosition = $.ligerDialog.open({
                target: $('#dlgUpdatePosition'),
                width: 320,
                height: 120,
                title: '修改职位',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formUpdatePosition.valid()) {
                                updatePosition();
                            }
                            else {
                                formUpdatePosition.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgUpdatePosition.hide();
                        }
                    }
                ]
            });
        }

        formUpdatePosition.setData(row);
    }

    function itemclick(item) {
        switch (item.method)
        {
            case 'add':
                showDlgAddPosition();
                break;

            case 'update':
                DoAction(gridPositions, function (row) {
                    showDlgUpdatePosition(row);
                }, '请选择需要修改的职位');
                break;

            case 'delete':
                DoActionConfirm(gridPositions, deletePosition, '请选择需要修删除的职位', '是否要删除当前选择的职位？');
                break;
        }
    }

    var toolbarPositions = {
        items: [
            { method: 'add', text: '增加', click: itemclick, icon: 'add' },
            { line: true },
            { method: 'update', text: '修改', click: itemclick, icon: 'modify' },
            { line: true },
            { method: 'delete', text: '删除', click: itemclick, icon: 'delete' }
        ]
    };

    var columnPositions = [
        { display: '', name: 'Id', align: 'left', hide: true },
        { display: '职位名', name: 'PositionName', align: 'left', width: 200, minwidth: 120 }
    ];

    function loadPositions() {
        var complete = false;
        $.post('/API/Position/Query', {}, function (data) {
            complete = true;
            gridPositions.loadData(data);
            gridPositions.gridloading.hide(100);
        }, 'json');
                
        var n = setInterval(function () {
            clearInterval(n);
            if (!complete) {                
                gridPositions.gridloading.show();
            }
        }, 100);        
    }

    function initForm() {
        formAddPosition = $('#formAddPosition').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { display: '职位名称', name: 'PositionName', newline: true, type: 'text', validate: { required: true } }
            ],
            validate: {}
        });

        formUpdatePosition = $('#formUpdatePosition').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { name: 'Id', type: 'hidden' },
                { display: '职位名称', name: 'PositionName', newline: true, type: 'text', validate: { required: true } }
            ],
            validate: {}
        });
    }

    $(document).ready(function () {
        $.metadata.setType("attr", "validate");
        initForm();
        gridPositions = $('#gridPositions').ligerGrid({
            title: '职位信息',
            columns: columnPositions,
            toolbar: toolbarPositions,
            resizable: false,
            frozen: false,
            rownumbers: true,
            dataAction: 'local'
        });

        loadPositions();
    });
</script>